package com.ruoyi.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.flowable.domain.bo.AppBo;
import com.ruoyi.flowable.domain.vo.AppVo;
import com.ruoyi.flowable.domain.App;
import com.ruoyi.flowable.mapper.AppMapper;
import com.ruoyi.flowable.service.IAppService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 事项Service业务层处理
 *
 * @author zxw
 * @date 2023-03-21
 */
@RequiredArgsConstructor
@Service
public class AppServiceImpl implements IAppService {

    private final AppMapper baseMapper;

    /**
     * 查询事项
     */
    @Override
    public AppVo queryById(Long appId){
        return baseMapper.selectVoById(appId);
    }

    /**
     * 查询事项列表
     */
    @Override
    public TableDataInfo<AppVo> queryPageList(AppBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<App> lqw = buildQueryWrapper(bo);
        Page<AppVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询事项列表
     */
    @Override
    public List<AppVo> queryList(AppBo bo) {
        LambdaQueryWrapper<App> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<App> buildQueryWrapper(AppBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<App> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAppName()), App::getAppName, bo.getAppName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), App::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), App::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增事项
     */
    @Override
    public Boolean insertByBo(AppBo bo) {
        App add = BeanUtil.toBean(bo, App.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAppId(add.getAppId());
        }
        return flag;
    }

    /**
     * 修改事项
     */
    @Override
    public Boolean updateByBo(AppBo bo) {
        App update = BeanUtil.toBean(bo, App.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(App entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除事项
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int updateStatus(Long appId) {
        AppVo app = baseMapper.selectVoById(appId);
        if (app.getStatus().equals("1")) {
            if (app.getFormId() == 0 || app.getUseVersion() == 0) {
                throw new ServiceException("未设置使用版本或主表单");
            }
            app.setStatus("0");
        } else {
            app.setStatus("1");
        }
        App update = BeanUtil.toBean(app, App.class);
        return baseMapper.updateById(update);
    }
}
